/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * File: LBG_Arctan_FOC.c
 *
 * Code generated for Simulink model 'LBG_Arctan_FOC'.
 *
 * Model version                  : 2.13
 * Simulink Coder version         : 9.8 (R2022b) 13-May-2022
 * C/C++ source code generated on : Mon Feb 10 18:51:01 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "LBG_Arctan_FOC.h"
#include "rtwtypes.h"
#include "LBG_Arctan_FOC_private.h"
#include <math.h>
#include "rt_nonfinite.h"
#include "rt_modf_snf.h"
#include "rt_atan2f_snf.h"

/* Named constants for Chart: '<S2>/Chart' */
#define LBG_Arctan_FOC_IN_CLL          ((uint8_T)1U)
#define LBG_Arctan_FOC_IN_Init         ((uint8_T)2U)
#define LBG_Arctan_FOC_IN_OPL          ((uint8_T)3U)

int_T LBG_Arctan_FOC_GlobalTID[3];
const rtTimingBridge *LBG_Arctan_FOC_TimingBrdg;

/* Exported block signals */
real32_T LGB_theta;                    /* '<S31>/Math Function' */
real32_T OB_LGB_SPD;                   /* '<S41>/Add1' */
MdlrefDW_LBG_Arctan_FOC_T LBG_Arctan_FOC_MdlrefDW;

/* Block signals (default storage) */
B_LBG_Arctan_FOC_c_T LBG_Arctan_FOC_B;

/* Block states (default storage) */
DW_LBG_Arctan_FOC_f_T LBG_Arctan_FOC_DW;

/*
 * Output and update for enable system:
 *    '<S17>/per Uint'
 *    '<S35>/per Uint'
 */
void LBG_Arctan_FOC_perUint(boolean_T rtu_Enable, real32_T rtu_In1, real32_T
  *rty_Out1)
{
  /* Outputs for Enabled SubSystem: '<S17>/per Uint' incorporates:
   *  EnablePort: '<S29>/Enable'
   */
  if (rtu_Enable) {
    real32_T rtb_Gain_b;

    /* Gain: '<S29>/Gain' */
    rtb_Gain_b = 0.159154937F * rtu_In1;

    /* Switch: '<S29>/Switch' incorporates:
     *  Bias: '<S29>/Bias'
     */
    if (rtb_Gain_b >= 0.0F) {
      *rty_Out1 = rtb_Gain_b;
    } else {
      *rty_Out1 = rtb_Gain_b + 1.0F;
    }

    /* End of Switch: '<S29>/Switch' */
  }

  /* End of Outputs for SubSystem: '<S17>/per Uint' */
}

/* System initialize for referenced model: 'LBG_Arctan_FOC' */
void LBG_Arctan_FOC_Init(void)
{
  /* SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
   *  SubSystem: '<S1>/Speed_Control'
   */
  /* SystemInitialize for Enabled SubSystem: '<S4>/Subsystem' */
  /* SystemInitialize for SignalConversion generated from: '<S171>/Id_Ref' */
  LBG_Arctan_FOC_B.OutportBufferForId_Ref = LBG_Arctan_FOC_ConstB.Constant;

  /* End of SystemInitialize for SubSystem: '<S4>/Subsystem' */
  /* End of SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator3' */

  /* SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/FOC'
   */
  /* Start for SwitchCase: '<S9>/Switch Case' */
  LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = -1;

  /* End of SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator4' */
}

/* Enable for referenced model: 'LBG_Arctan_FOC' */
void LBG_Arctan_FOC_Enable(void)
{
  /* Enable for S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
   *  SubSystem: '<S1>/10ms_Task'
   */
  /* Enable for Chart: '<S2>/Chart' */
  LBG_Arctan_FOC_DW.presentTicks = rtmGetClockTick2();
  LBG_Arctan_FOC_DW.previousTicks = LBG_Arctan_FOC_DW.presentTicks;

  /* End of Enable for S-Function (fcgen): '<S1>/Function-Call Generator5' */

  /* Enable for S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/FOC'
   */
  LBG_Arctan_FOC_DW.FOC_RESET_ELAPS_T = true;

  /* Enable for DiscreteIntegrator: '<S87>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE = 1U;

  /* Enable for DiscreteIntegrator: '<S135>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_m = 1U;

  /* End of Enable for S-Function (fcgen): '<S1>/Function-Call Generator4' */
}

/* Disable for referenced model: 'LBG_Arctan_FOC' */
void LBG_Arctan_FOC_Disable(void)
{
  /* Disable for S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
   *  SubSystem: '<S1>/10ms_Task'
   */
  /* Disable for Chart: '<S2>/Chart' */
  LBG_Arctan_FOC_DW.presentTicks = rtmGetClockTick2();
  LBG_Arctan_FOC_DW.elapsedTicks = LBG_Arctan_FOC_DW.presentTicks -
    LBG_Arctan_FOC_DW.previousTicks;
  LBG_Arctan_FOC_DW.previousTicks = LBG_Arctan_FOC_DW.presentTicks;
  if (LBG_Arctan_FOC_DW.temporalCounter_i1 + LBG_Arctan_FOC_DW.elapsedTicks <=
      63U) {
    LBG_Arctan_FOC_DW.temporalCounter_i1 = (uint8_T)
      (LBG_Arctan_FOC_DW.temporalCounter_i1 + LBG_Arctan_FOC_DW.elapsedTicks);
  } else {
    LBG_Arctan_FOC_DW.temporalCounter_i1 = 63U;
  }

  /* End of Disable for Chart: '<S2>/Chart' */
  /* End of Disable for S-Function (fcgen): '<S1>/Function-Call Generator5' */

  /* Disable for S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
   *  SubSystem: '<S1>/Speed_Control'
   */
  /* Disable for Enabled SubSystem: '<S4>/Subsystem' */
  LBG_Arctan_FOC_DW.Subsystem_MODE = false;

  /* End of Disable for SubSystem: '<S4>/Subsystem' */
  /* End of Disable for S-Function (fcgen): '<S1>/Function-Call Generator3' */

  /* Disable for S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/FOC'
   */
  /* Disable for SwitchCase: '<S9>/Switch Case' */
  LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = -1;

  /* End of Disable for S-Function (fcgen): '<S1>/Function-Call Generator4' */
}

/* Output and update for referenced model: 'LBG_Arctan_FOC' */
void LBG_Arctan_FOC_step(real32_T arg_Target_Speed, real32_T arg_Iabc[3],
  real32_T arg_Real_Theta, real32_T arg_Vabc_PU[3])
{
  real_T tmp;
  real32_T rtb_Add1_a;
  real32_T rtb_Add3_p;
  real32_T rtb_Saturation;
  real32_T rtb_Saturation_n;
  real32_T rtb_Sum4;
  real32_T rtb_Sum_b;
  real32_T rtb_Switch_idx_0;
  real32_T rtb_sum_alpha;
  uint16_T rtb_Get_Integer;
  int8_T rtPrevAction;

  /* DataStoreWrite: '<S1>/Data Store Write' */
  LBG_Arctan_FOC_DW.Real_Theta = arg_Real_Theta;
  if (rtmIsSampleHit(2, 0)) {
    /* S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
     *  SubSystem: '<S1>/10ms_Task'
     */
    /* Chart: '<S2>/Chart' */
    LBG_Arctan_FOC_DW.presentTicks = rtmGetClockTick2();
    LBG_Arctan_FOC_DW.elapsedTicks = LBG_Arctan_FOC_DW.presentTicks -
      LBG_Arctan_FOC_DW.previousTicks;
    LBG_Arctan_FOC_DW.previousTicks = LBG_Arctan_FOC_DW.presentTicks;
    if (LBG_Arctan_FOC_DW.temporalCounter_i1 + LBG_Arctan_FOC_DW.elapsedTicks <=
        63U) {
      LBG_Arctan_FOC_DW.temporalCounter_i1 = (uint8_T)
        (LBG_Arctan_FOC_DW.temporalCounter_i1 + LBG_Arctan_FOC_DW.elapsedTicks);
    } else {
      LBG_Arctan_FOC_DW.temporalCounter_i1 = 63U;
    }

    if (LBG_Arctan_FOC_DW.is_active_c1_LBG_Arctan_FOC == 0U) {
      LBG_Arctan_FOC_DW.is_active_c1_LBG_Arctan_FOC = 1U;
      LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_Init;
      LBG_Arctan_FOC_DW.temporalCounter_i1 = 0U;
      LBG_Arctan_FOC_B.Motor_State = 0.0;
    } else {
      switch (LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC) {
       case LBG_Arctan_FOC_IN_CLL:
        LBG_Arctan_FOC_B.Motor_State = 2.0;
        LBG_Arctan_FOC_DW.time = 0.0;
        break;

       case LBG_Arctan_FOC_IN_Init:
        if (LBG_Arctan_FOC_DW.temporalCounter_i1 >= 50U) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_OPL;
          LBG_Arctan_FOC_B.Motor_State = 1.0;
          LBG_Arctan_FOC_DW.time++;
        } else {
          LBG_Arctan_FOC_B.Motor_State = 0.0;
        }
        break;

       default:
        /* case IN_OPL: */
        if ((LBG_Arctan_FOC_DW.time > 100.0) && (LBG_Arctan_FOC_DW.Delay_DSTATE >
             900.0F)) {
          LBG_Arctan_FOC_DW.is_c1_LBG_Arctan_FOC = LBG_Arctan_FOC_IN_CLL;
          LBG_Arctan_FOC_B.Motor_State = 2.0;
          LBG_Arctan_FOC_DW.time = 0.0;
        } else {
          LBG_Arctan_FOC_B.Motor_State = 1.0;
          LBG_Arctan_FOC_DW.time++;
        }
        break;
      }
    }

    /* End of Chart: '<S2>/Chart' */
    /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator5' */
  }

  if (rtmIsSampleHit(1, 0)) {
    /* S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
     *  SubSystem: '<S1>/Speed_Control'
     */
    /* Outputs for Enabled SubSystem: '<S4>/Subsystem' incorporates:
     *  EnablePort: '<S171>/Enable'
     */
    /* RelationalOperator: '<S170>/Compare' incorporates:
     *  Constant: '<S170>/Constant'
     */
    if (LBG_Arctan_FOC_B.Motor_State == 2.0) {
      if (!LBG_Arctan_FOC_DW.Subsystem_MODE) {
        LBG_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T = true;

        /* Enable for DiscreteIntegrator: '<S204>/Integrator' */
        LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h = 1U;
        LBG_Arctan_FOC_DW.Subsystem_MODE = true;
      }

      if (LBG_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T) {
        LBG_Arctan_FOC_DW.Subsystem_ELAPS_T = 0U;
      } else {
        LBG_Arctan_FOC_DW.Subsystem_ELAPS_T = rtmGetClockTick1() -
          LBG_Arctan_FOC_DW.Subsystem_PREV_T;
      }

      LBG_Arctan_FOC_DW.Subsystem_PREV_T = rtmGetClockTick1();
      LBG_Arctan_FOC_DW.Subsystem_RESET_ELAPS_T = false;

      /* SignalConversion generated from: '<S171>/Id_Ref' */
      LBG_Arctan_FOC_B.OutportBufferForId_Ref = LBG_Arctan_FOC_ConstB.Constant;

      /* Sum: '<S171>/Sum' */
      rtb_Sum_b = arg_Target_Speed - LBG_Arctan_FOC_DW.Delay_DSTATE;

      /* DiscreteIntegrator: '<S204>/Integrator' */
      if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h == 0) {
        /* DiscreteIntegrator: '<S204>/Integrator' */
        LBG_Arctan_FOC_DW.Integrator_DSTATE_c += 0.002F * (real32_T)
          LBG_Arctan_FOC_DW.Subsystem_ELAPS_T
          * LBG_Arctan_FOC_DW.Integrator_PREV_U_a;
      }

      /* End of DiscreteIntegrator: '<S204>/Integrator' */

      /* Sum: '<S213>/Sum' incorporates:
       *  Gain: '<S209>/Proportional Gain'
       */
      LBG_Arctan_FOC_B.Saturation = 0.000264109141F * rtb_Sum_b +
        LBG_Arctan_FOC_DW.Integrator_DSTATE_c;

      /* Saturate: '<S211>/Saturation' */
      if (LBG_Arctan_FOC_B.Saturation > 3.26732683F) {
        /* Sum: '<S213>/Sum' incorporates:
         *  Saturate: '<S211>/Saturation'
         */
        LBG_Arctan_FOC_B.Saturation = 3.26732683F;
      } else if (LBG_Arctan_FOC_B.Saturation < -3.26732683F) {
        /* Sum: '<S213>/Sum' incorporates:
         *  Saturate: '<S211>/Saturation'
         */
        LBG_Arctan_FOC_B.Saturation = -3.26732683F;
      }

      /* End of Saturate: '<S211>/Saturation' */

      /* Update for DiscreteIntegrator: '<S204>/Integrator' incorporates:
       *  Gain: '<S201>/Integral Gain'
       */
      LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_h = 0U;
      LBG_Arctan_FOC_DW.Integrator_PREV_U_a = 0.00417593215F * rtb_Sum_b;
    } else {
      LBG_Arctan_FOC_DW.Subsystem_MODE = false;
    }

    /* End of RelationalOperator: '<S170>/Compare' */
    /* End of Outputs for SubSystem: '<S4>/Subsystem' */
    /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator3' */
  }

  /* S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/FOC'
   */
  if (LBG_Arctan_FOC_DW.FOC_RESET_ELAPS_T) {
    LBG_Arctan_FOC_DW.FOC_ELAPS_T = 0U;
  } else {
    LBG_Arctan_FOC_DW.FOC_ELAPS_T = rtmGetClockTick0() -
      LBG_Arctan_FOC_DW.FOC_PREV_T;
  }

  /* Update for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
   *  ActionPort: '<S168>/Action Port'
   */
  /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
   *  ActionPort: '<S168>/Action Port'
   */
  /* SwitchCase: '<S9>/Switch Case' */
  LBG_Arctan_FOC_DW.FOC_PREV_T = rtmGetClockTick0();

  /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem1' */
  /* End of Update for SubSystem: '<S9>/Switch Case Action Subsystem1' */
  LBG_Arctan_FOC_DW.FOC_RESET_ELAPS_T = false;

  /* Outputs for Atomic SubSystem: '<S7>/Clarke Transform' */
  /* Gain: '<S48>/one_by_sqrt3' incorporates:
   *  Sum: '<S48>/a_plus_2b'
   */
  rtb_sum_alpha = ((arg_Iabc[0] + arg_Iabc[1]) + arg_Iabc[1]) * 0.577350259F;

  /* End of Outputs for SubSystem: '<S7>/Clarke Transform' */

  /* Saturate: '<S10>/Saturation' incorporates:
   *  Delay: '<S10>/Delay'
   */
  if (LBG_Arctan_FOC_DW.Delay_DSTATE_n > 3000.0F) {
    /* MinMax: '<S156>/Min' */
    LBG_Arctan_FOC_DW.Delay_DSTATE_n = 3000.0F;
  } else if (LBG_Arctan_FOC_DW.Delay_DSTATE_n < 10.0F) {
    /* MinMax: '<S156>/Min' */
    LBG_Arctan_FOC_DW.Delay_DSTATE_n = 10.0F;
  }

  /* End of Saturate: '<S10>/Saturation' */

  /* SwitchCase: '<S9>/Switch Case' */
  rtPrevAction = LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem;
  LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = -1;
  tmp = trunc(LBG_Arctan_FOC_B.Motor_State);
  if (rtIsNaN(tmp) || rtIsInf(tmp)) {
    tmp = 0.0;
  } else {
    tmp = fmod(tmp, 4.294967296E+9);
  }

  switch (tmp < 0.0 ? -(int32_T)(uint32_T)-tmp : (int32_T)(uint32_T)tmp) {
   case 0:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 0;
    break;

   case 1:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 1;
    break;

   case 2:
    LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem = 2;
    break;
  }

  switch (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem) {
   case 0:
    /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem' incorporates:
     *  ActionPort: '<S167>/Action Port'
     */
    /* Merge: '<S9>/Merge' incorporates:
     *  Constant: '<S167>/Constant1'
     *  SignalConversion generated from: '<S167>/Id_Ref'
     */
    LBG_Arctan_FOC_B.Merge = 1.2F;

    /* Merge: '<S9>/Merge1' incorporates:
     *  Constant: '<S167>/Constant'
     *  SignalConversion generated from: '<S167>/Iq_Ref'
     */
    LBG_Arctan_FOC_B.Merge1 = 0.0F;

    /* Merge: '<S9>/Merge2' incorporates:
     *  Constant: '<S167>/Constant2'
     *  SignalConversion generated from: '<S167>/Theta'
     */
    LBG_Arctan_FOC_B.Merge2 = 0.0F;

    /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem' */
    break;

   case 1:
    if (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem != rtPrevAction) {
      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE = true;

      /* Enable for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
       *  ActionPort: '<S168>/Action Port'
       */
      /* Enable for SwitchCase: '<S9>/Switch Case' incorporates:
       *  DiscreteIntegrator: '<S168>/Discrete-Time Integrator'
       *  DiscreteIntegrator: '<S168>/Discrete-Time Integrator1'
       */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTEM_ = 1U;
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_E = 1U;

      /* End of Enable for SubSystem: '<S9>/Switch Case Action Subsystem1' */
    }

    /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
     *  ActionPort: '<S168>/Action Port'
     */
    if (LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE) {
      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_ELAP = 0U;
    } else {
      LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_ELAP =
        LBG_Arctan_FOC_DW.FOC_PREV_T
        - LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_PREV;
    }

    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_PREV =
      LBG_Arctan_FOC_DW.FOC_PREV_T;
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE = false;

    /* DiscreteIntegrator: '<S168>/Discrete-Time Integrator1' */
    if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTEM_ == 0) {
      /* DiscreteIntegrator: '<S168>/Discrete-Time Integrator1' */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_DSTATE += 5.0E-5F * (real32_T)
        LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_ELAP *
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_PREV_U;
    }

    /* End of DiscreteIntegrator: '<S168>/Discrete-Time Integrator1' */

    /* Math: '<S168>/Math Function' incorporates:
     *  Constant: '<S168>/Constant4'
     *  DataStoreWrite: '<S168>/Data Store Write'
     */
    LBG_Arctan_FOC_DW.OPL_Theta = rt_modf_snf
      (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_DSTATE, 6.28318548F);

    /* Merge: '<S9>/Merge' incorporates:
     *  Constant: '<S168>/Constant1'
     *  SignalConversion generated from: '<S168>/Id_Ref'
     */
    LBG_Arctan_FOC_B.Merge = 1.2F;

    /* Merge: '<S9>/Merge1' incorporates:
     *  Constant: '<S168>/Constant'
     *  SignalConversion generated from: '<S168>/Iq_Ref'
     */
    LBG_Arctan_FOC_B.Merge1 = 0.0F;

    /* DiscreteIntegrator: '<S168>/Discrete-Time Integrator' */
    if (LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_E == 0) {
      /* DiscreteIntegrator: '<S168>/Discrete-Time Integrator' */
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE += 5.0E-5F * (real32_T)
        LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_ELAP *
        LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U;
    }

    /* End of DiscreteIntegrator: '<S168>/Discrete-Time Integrator' */

    /* Merge: '<S9>/Merge2' incorporates:
     *  DataStoreWrite: '<S168>/Data Store Write'
     *  SignalConversion generated from: '<S168>/Theta'
     */
    LBG_Arctan_FOC_B.Merge2 = LBG_Arctan_FOC_DW.OPL_Theta;

    /* Update for DiscreteIntegrator: '<S168>/Discrete-Time Integrator1' incorporates:
     *  Gain: '<S168>/Gain'
     *  Gain: '<S168>/Gain1'
     */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_SYSTEM_ = 0U;
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator1_PREV_U = 0.116666667F *
      LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_DSTATE * 6.28318548F;

    /* Update for DiscreteIntegrator: '<S168>/Discrete-Time Integrator' */
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_SYSTEM_E = 0U;
    LBG_Arctan_FOC_DW.DiscreteTimeIntegrator_PREV_U =
      LBG_Arctan_FOC_ConstB.Divide;

    /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem1' */
    break;

   case 2:
    /* Outputs for IfAction SubSystem: '<S9>/Switch Case Action Subsystem2' incorporates:
     *  ActionPort: '<S169>/Action Port'
     */
    /* Merge: '<S9>/Merge' incorporates:
     *  SignalConversion generated from: '<S169>/Id'
     */
    LBG_Arctan_FOC_B.Merge = LBG_Arctan_FOC_B.OutportBufferForId_Ref;

    /* Merge: '<S9>/Merge1' incorporates:
     *  SignalConversion generated from: '<S169>/Iq'
     */
    LBG_Arctan_FOC_B.Merge1 = LBG_Arctan_FOC_B.Saturation;

    /* Merge: '<S9>/Merge2' incorporates:
     *  Delay: '<S3>/Delay'
     *  SignalConversion generated from: '<S169>/Obs_Theta'
     */
    LBG_Arctan_FOC_B.Merge2 = LGB_theta;

    /* End of Outputs for SubSystem: '<S9>/Switch Case Action Subsystem2' */
    break;
  }

  /* Gain: '<S161>/convert_pu' */
  rtb_Sum_b = 0.159154937F * LBG_Arctan_FOC_B.Merge2;

  /* If: '<S161>/If' incorporates:
   *  Constant: '<S162>/Constant'
   *  RelationalOperator: '<S162>/Compare'
   */
  if (rtb_Sum_b < 0.0F) {
    /* Outputs for IfAction SubSystem: '<S161>/If Action Subsystem' incorporates:
     *  ActionPort: '<S163>/Action Port'
     */
    /* DataTypeConversion: '<S163>/Convert_uint16' */
    rtb_Saturation = floorf(rtb_Sum_b);
    if (rtIsInfF(rtb_Saturation)) {
      rtb_Saturation = 0.0F;
    } else {
      rtb_Saturation = fmodf(rtb_Saturation, 65536.0F);
    }

    /* Sum: '<S163>/Sum' incorporates:
     *  DataTypeConversion: '<S163>/Convert_back'
     *  DataTypeConversion: '<S163>/Convert_uint16'
     */
    rtb_Add1_a = rtb_Sum_b - (real32_T)(rtb_Saturation < 0.0F ? (int32_T)
      (int16_T)-(int16_T)(uint16_T)-rtb_Saturation : (int32_T)(int16_T)(uint16_T)
      rtb_Saturation);

    /* End of Outputs for SubSystem: '<S161>/If Action Subsystem' */
  } else {
    /* Outputs for IfAction SubSystem: '<S161>/If Action Subsystem1' incorporates:
     *  ActionPort: '<S164>/Action Port'
     */
    /* DataTypeConversion: '<S164>/Convert_uint16' */
    rtb_Saturation = truncf(rtb_Sum_b);
    if (rtIsNaNF(rtb_Saturation) || rtIsInfF(rtb_Saturation)) {
      rtb_Saturation = 0.0F;
    } else {
      rtb_Saturation = fmodf(rtb_Saturation, 65536.0F);
    }

    /* Sum: '<S164>/Sum' incorporates:
     *  DataTypeConversion: '<S164>/Convert_back'
     *  DataTypeConversion: '<S164>/Convert_uint16'
     */
    rtb_Add1_a = rtb_Sum_b - (real32_T)(int16_T)(uint16_T)rtb_Saturation;

    /* End of Outputs for SubSystem: '<S161>/If Action Subsystem1' */
  }

  /* End of If: '<S161>/If' */

  /* Gain: '<S54>/indexing' */
  rtb_Add1_a *= 800.0F;

  /* DataTypeConversion: '<S54>/Get_Integer' */
  rtb_Saturation = truncf(rtb_Add1_a);
  if (rtIsNaNF(rtb_Saturation) || rtIsInfF(rtb_Saturation)) {
    rtb_Saturation = 0.0F;
  } else {
    rtb_Saturation = fmodf(rtb_Saturation, 65536.0F);
  }

  rtb_Get_Integer = (uint16_T)(rtb_Saturation < 0.0F ? (int32_T)(uint16_T)
    -(int16_T)(uint16_T)-rtb_Saturation : (int32_T)(uint16_T)rtb_Saturation);

  /* End of DataTypeConversion: '<S54>/Get_Integer' */

  /* Sum: '<S54>/Sum2' incorporates:
   *  DataTypeConversion: '<S54>/Data Type Conversion1'
   */
  rtb_Sum_b = rtb_Add1_a - (real32_T)rtb_Get_Integer;

  /* Selector: '<S54>/Lookup' incorporates:
   *  Constant: '<S54>/sine_table_values'
   *  Sum: '<S54>/Sum'
   */
  rtb_Add3_p = rtCP_sine_table_values_Value[rtb_Get_Integer];

  /* Sum: '<S160>/Sum4' incorporates:
   *  Constant: '<S54>/offset'
   *  Constant: '<S54>/sine_table_values'
   *  Product: '<S160>/Product'
   *  Selector: '<S54>/Lookup'
   *  Sum: '<S160>/Sum3'
   *  Sum: '<S54>/Sum'
   */
  rtb_Sum4 = (rtCP_sine_table_values_Value[(int32_T)(rtb_Get_Integer + 1U)] -
              rtb_Add3_p) * rtb_Sum_b + rtb_Add3_p;

  /* Selector: '<S54>/Lookup' incorporates:
   *  Constant: '<S54>/offset'
   *  Constant: '<S54>/sine_table_values'
   *  Sum: '<S160>/Sum5'
   *  Sum: '<S54>/Sum'
   */
  rtb_Add3_p = rtCP_sine_table_values_Value[(int32_T)(rtb_Get_Integer + 200U)];

  /* Sum: '<S160>/Sum6' incorporates:
   *  Constant: '<S54>/offset'
   *  Constant: '<S54>/sine_table_values'
   *  Product: '<S160>/Product1'
   *  Selector: '<S54>/Lookup'
   *  Sum: '<S160>/Sum5'
   *  Sum: '<S54>/Sum'
   */
  rtb_Add1_a = (rtCP_sine_table_values_Value[(int32_T)(rtb_Get_Integer + 201U)]
                - rtb_Add3_p) * rtb_Sum_b + rtb_Add3_p;

  /* Outputs for Atomic SubSystem: '<S7>/Park Transform' */
  /* Outputs for Atomic SubSystem: '<S7>/Clarke Transform' */
  /* Sum: '<S7>/Sum' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Product: '<S53>/acos'
   *  Product: '<S53>/bsin'
   *  Sum: '<S53>/sum_Ds'
   */
  rtb_Sum_b = LBG_Arctan_FOC_B.Merge - (arg_Iabc[0] * rtb_Add1_a + rtb_sum_alpha
    * rtb_Sum4);

  /* End of Outputs for SubSystem: '<S7>/Clarke Transform' */
  /* End of Outputs for SubSystem: '<S7>/Park Transform' */

  /* DiscreteIntegrator: '<S87>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE == 0) {
    /* DiscreteIntegrator: '<S87>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE += 5.0E-5F * (real32_T)
      LBG_Arctan_FOC_DW.FOC_ELAPS_T * LBG_Arctan_FOC_DW.Integrator_PREV_U;
  }

  /* End of DiscreteIntegrator: '<S87>/Integrator' */

  /* Sum: '<S96>/Sum' incorporates:
   *  Gain: '<S92>/Proportional Gain'
   */
  rtb_Saturation = 0.485F * rtb_Sum_b + LBG_Arctan_FOC_DW.Integrator_DSTATE;

  /* Saturate: '<S94>/Saturation' */
  if (rtb_Saturation > 6.92820311F) {
    rtb_Saturation = 6.92820311F;
  } else if (rtb_Saturation < -6.92820311F) {
    rtb_Saturation = -6.92820311F;
  }

  /* End of Saturate: '<S94>/Saturation' */

  /* Outputs for Atomic SubSystem: '<S7>/Park Transform' */
  /* Outputs for Atomic SubSystem: '<S7>/Clarke Transform' */
  /* Sum: '<S7>/Sum1' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Product: '<S53>/asin'
   *  Product: '<S53>/bcos'
   *  Sum: '<S53>/sum_Qs'
   */
  rtb_Add3_p = LBG_Arctan_FOC_B.Merge1 - (rtb_sum_alpha * rtb_Add1_a - arg_Iabc
    [0] * rtb_Sum4);

  /* End of Outputs for SubSystem: '<S7>/Clarke Transform' */
  /* End of Outputs for SubSystem: '<S7>/Park Transform' */

  /* DiscreteIntegrator: '<S135>/Integrator' */
  if (LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_m == 0) {
    /* DiscreteIntegrator: '<S135>/Integrator' */
    LBG_Arctan_FOC_DW.Integrator_DSTATE_m += 5.0E-5F * (real32_T)
      LBG_Arctan_FOC_DW.FOC_ELAPS_T * LBG_Arctan_FOC_DW.Integrator_PREV_U_b;
  }

  /* End of DiscreteIntegrator: '<S135>/Integrator' */

  /* Sum: '<S144>/Sum' incorporates:
   *  Gain: '<S140>/Proportional Gain'
   */
  rtb_Saturation_n = 0.485F * rtb_Add3_p + LBG_Arctan_FOC_DW.Integrator_DSTATE_m;

  /* Saturate: '<S142>/Saturation' */
  if (rtb_Saturation_n > 6.92820311F) {
    rtb_Saturation_n = 6.92820311F;
  } else if (rtb_Saturation_n < -6.92820311F) {
    rtb_Saturation_n = -6.92820311F;
  }

  /* End of Saturate: '<S142>/Saturation' */

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* Switch: '<S152>/Switch' incorporates:
   *  Product: '<S51>/dcos'
   *  Product: '<S51>/dsin'
   *  Product: '<S51>/qcos'
   *  Product: '<S51>/qsin'
   *  Sum: '<S51>/sum_alpha'
   *  Sum: '<S51>/sum_beta'
   */
  rtb_Switch_idx_0 = rtb_Saturation * rtb_Add1_a - rtb_Saturation_n * rtb_Sum4;
  rtb_Sum4 = rtb_Saturation_n * rtb_Add1_a + rtb_Saturation * rtb_Sum4;

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */

  /* Outputs for Atomic SubSystem: '<S7>/Clarke Transform' */
  /* Product: '<S12>/Product2' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Product: '<S12>/Product8'
   */
  rtb_Add1_a = rtb_sum_alpha * LBG_Arctan_FOC_DW.Delay_DSTATE_n;

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* Sum: '<S12>/Add4' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   *  Delay: '<S12>/Delay'
   *  Gain: '<S12>/Gain1'
   *  Gain: '<S12>/Gain2'
   *  Gain: '<S12>/Gain3'
   *  Gain: '<S12>/Gain4'
   *  Product: '<S12>/Product'
   *  Product: '<S12>/Product1'
   *  Product: '<S12>/Product2'
   *  Product: '<S12>/Product3'
   *  Sum: '<S12>/Add2'
   *  Sum: '<S12>/Add3'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_a = ((0.1F * arg_Iabc[0] - rtb_Add1_a * 5.0E-5F)
    * LBG_Arctan_FOC_ConstB.Add1 + LBG_Arctan_FOC_DW.Delay_DSTATE_a *
    LBG_Arctan_FOC_ConstB.Add) + (0.1F * rtb_Switch_idx_0 -
    LBG_Arctan_FOC_DW.Delay_DSTATE_n * rtb_Sum4 * 5.0E-5F);

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */

  /* Product: '<S12>/Product6' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Product: '<S12>/Product9'
   */
  rtb_Saturation = arg_Iabc[0] * LBG_Arctan_FOC_DW.Delay_DSTATE_n;

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* Sum: '<S12>/Add9' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   *  Delay: '<S12>/Delay1'
   *  Gain: '<S12>/Gain6'
   *  Gain: '<S12>/Gain7'
   *  Gain: '<S12>/Gain8'
   *  Gain: '<S12>/Gain9'
   *  Product: '<S12>/Product4'
   *  Product: '<S12>/Product5'
   *  Product: '<S12>/Product6'
   *  Product: '<S12>/Product7'
   *  Sum: '<S12>/Add7'
   *  Sum: '<S12>/Add8'
   */
  LBG_Arctan_FOC_DW.Delay1_DSTATE = ((rtb_Saturation * 5.0E-5F + 0.1F *
    rtb_sum_alpha) * LBG_Arctan_FOC_ConstB.Add6 +
    LBG_Arctan_FOC_DW.Delay1_DSTATE * LBG_Arctan_FOC_ConstB.Add5) +
    (LBG_Arctan_FOC_DW.Delay_DSTATE_n * rtb_Switch_idx_0 * 5.0E-5F + 0.1F *
     rtb_Sum4);

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */
  /* End of Outputs for SubSystem: '<S7>/Clarke Transform' */

  /* MinMax: '<S156>/Min' incorporates:
   *  Constant: '<S23>/One'
   *  Product: '<S23>/Product1'
   *  Sum: '<S23>/Add1'
   *  UnitDelay: '<S23>/Unit Delay'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_n = 0.98F * LBG_Arctan_FOC_DW.UnitDelay_DSTATE
    + LBG_Arctan_FOC_ConstB.Product;

  /* Outputs for Atomic SubSystem: '<S13>/atan2' */
  /* Outputs for Atomic SubSystem: '<S7>/Clarke Transform' */
  /* Trigonometry: '<S17>/Atan2' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Delay: '<S12>/Delay'
   *  Delay: '<S12>/Delay1'
   *  Gain: '<S12>/Gain10'
   *  Gain: '<S12>/Gain11'
   *  Gain: '<S12>/Gain12'
   *  Gain: '<S12>/Gain13'
   *  Sum: '<S12>/Add10'
   *  Sum: '<S12>/Add11'
   */
  rtb_Add1_a = rt_atan2f_snf((LBG_Arctan_FOC_DW.Delay_DSTATE_a - 0.194F *
    arg_Iabc[0]) + rtb_Add1_a * 9.7E-5F, (LBG_Arctan_FOC_DW.Delay1_DSTATE -
    0.194F * rtb_sum_alpha) - rtb_Saturation * 9.7E-5F);

  /* End of Outputs for SubSystem: '<S7>/Clarke Transform' */

  /* Outputs for Enabled SubSystem: '<S17>/If Action Subsystem' incorporates:
   *  EnablePort: '<S28>/Enable'
   */
  /* Merge: '<S17>/Merge' incorporates:
   *  SignalConversion generated from: '<S28>/In1'
   */
  rtb_Saturation = rtb_Add1_a;

  /* End of Outputs for SubSystem: '<S17>/If Action Subsystem' */

  /* Outputs for Enabled SubSystem: '<S17>/per Uint' */
  /* RelationalOperator: '<S27>/Compare' incorporates:
   *  Constant: '<S17>/Constant'
   *  Constant: '<S27>/Constant'
   */
  LBG_Arctan_FOC_perUint(false, rtb_Add1_a, &rtb_Saturation);

  /* End of Outputs for SubSystem: '<S17>/per Uint' */
  /* End of Outputs for SubSystem: '<S13>/atan2' */

  /* Delay: '<S30>/Delay2' */
  rtb_Saturation = LBG_Arctan_FOC_DW.Delay2_DSTATE;

  /* Saturate: '<S11>/Saturation' incorporates:
   *  Delay: '<S11>/Delay'
   */
  if (OB_LGB_SPD > LGB_SPD_MAX) {
    rtb_Add1_a = LGB_SPD_MAX;
  } else if (OB_LGB_SPD < 10.0F) {
    rtb_Add1_a = 10.0F;
  } else {
    rtb_Add1_a = OB_LGB_SPD;
  }

  /* End of Saturate: '<S11>/Saturation' */

  /* Delay: '<S30>/Delay3' */
  rtb_Saturation_n = LBG_Arctan_FOC_DW.Delay3_DSTATE;

  /* Outputs for Atomic SubSystem: '<S7>/Clarke Transform' */
  /* Sum: '<S30>/Add10' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Constant: '<S30>/Constant2'
   *  Delay: '<S30>/Delay'
   *  Delay: '<S30>/Delay2'
   *  Delay: '<S30>/Delay3'
   *  Gain: '<S30>/Gain'
   *  Product: '<S30>/Product1'
   *  Product: '<S30>/Product8'
   *  Sum: '<S30>/Add1'
   */
  LBG_Arctan_FOC_DW.Delay2_DSTATE = (LBG_Arctan_FOC_DW.Delay2_DSTATE - 5.0E-5F *
    rtb_Add1_a * LBG_Arctan_FOC_DW.Delay3_DSTATE) +
    (LBG_Arctan_FOC_DW.Delay_DSTATE_p - arg_Iabc[0]) * 0.05F;

  /* Sum: '<S30>/Add3' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  Constant: '<S30>/Constant3'
   *  Delay: '<S30>/Delay1'
   *  Delay: '<S30>/Delay3'
   *  Gain: '<S30>/Gain1'
   *  Product: '<S30>/Product2'
   *  Product: '<S30>/Product3'
   *  Sum: '<S30>/Add2'
   */
  LBG_Arctan_FOC_DW.Delay3_DSTATE = (5.0E-5F * rtb_Add1_a * rtb_Saturation +
    LBG_Arctan_FOC_DW.Delay3_DSTATE) + (LBG_Arctan_FOC_DW.Delay1_DSTATE_e -
    rtb_sum_alpha) * 0.05F;

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* Sum: '<S30>/Add4' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   *  Constant: '<S30>/Constant10'
   *  Constant: '<S30>/Constant11'
   *  Constant: '<S30>/Constant9'
   *  Delay: '<S30>/Delay'
   *  Product: '<S30>/Product'
   *  Product: '<S30>/Product10'
   *  Product: '<S30>/Product11'
   *  Product: '<S30>/Product12'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_p = ((LBG_Arctan_FOC_DW.Delay_DSTATE_p *
    LBG_Arctan_FOC_ConstB.Add_p - 0.515463889F * rtb_Saturation) + 0.515463889F *
    rtb_Switch_idx_0) - -0.005F * arg_Iabc[0];

  /* Sum: '<S30>/Add6' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S48>/a16'
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   *  Constant: '<S30>/Constant13'
   *  Constant: '<S30>/Constant6'
   *  Constant: '<S30>/Constant7'
   *  Delay: '<S30>/Delay1'
   *  Product: '<S30>/Product4'
   *  Product: '<S30>/Product5'
   *  Product: '<S30>/Product6'
   *  Product: '<S30>/Product7'
   */
  LBG_Arctan_FOC_DW.Delay1_DSTATE_e = ((LBG_Arctan_FOC_DW.Delay1_DSTATE_e *
    LBG_Arctan_FOC_ConstB.Add5_b - 0.515463889F * rtb_Saturation_n) +
    0.515463889F * rtb_Sum4) - -0.005F * rtb_sum_alpha;

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */
  /* End of Outputs for SubSystem: '<S7>/Clarke Transform' */

  /* Outputs for Atomic SubSystem: '<S31>/atan2' */
  /* Trigonometry: '<S35>/Atan2' incorporates:
   *  Delay: '<S30>/Delay2'
   *  Delay: '<S30>/Delay3'
   */
  rtb_sum_alpha = rt_atan2f_snf(LBG_Arctan_FOC_DW.Delay2_DSTATE,
    LBG_Arctan_FOC_DW.Delay3_DSTATE);

  /* Outputs for Enabled SubSystem: '<S35>/If Action Subsystem' incorporates:
   *  EnablePort: '<S46>/Enable'
   */
  /* Merge: '<S35>/Merge' incorporates:
   *  SignalConversion generated from: '<S46>/In1'
   */
  LBG_Arctan_FOC_B.Merge_n = rtb_sum_alpha;

  /* End of Outputs for SubSystem: '<S35>/If Action Subsystem' */

  /* Outputs for Enabled SubSystem: '<S35>/per Uint' */
  /* RelationalOperator: '<S45>/Compare' incorporates:
   *  Constant: '<S35>/Constant'
   *  Constant: '<S45>/Constant'
   */
  LBG_Arctan_FOC_perUint(false, rtb_sum_alpha, &LBG_Arctan_FOC_B.Merge_n);

  /* End of Outputs for SubSystem: '<S35>/per Uint' */

  /* Math: '<S31>/Math Function' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S35>/a16'
   *  Constant: '<S31>/Constant'
   *  Gain: '<S31>/Gain'
   */
  LGB_theta = rt_modf_snf(-LBG_Arctan_FOC_B.Merge_n, 6.28318548F);

  /* End of Outputs for SubSystem: '<S31>/atan2' */

  /* Gain: '<S34>/PositionToCount' */
  rtb_Saturation = truncf(6.83563648E+8F * LGB_theta);
  if (rtIsNaNF(rtb_Saturation)) {
    rtb_Saturation = 0.0F;
  } else {
    rtb_Saturation = fmodf(rtb_Saturation, 4.2949673E+9F);
  }

  /* Sum: '<S38>/Add1' incorporates:
   *  Constant: '<S38>/Filter_Constant'
   *  Constant: '<S38>/One'
   *  DataTypeConversion: '<S43>/DTC'
   *  Delay: '<S34>/Delay'
   *  Gain: '<S31>/Gain1'
   *  Gain: '<S34>/PositionToCount'
   *  Gain: '<S34>/SpeedGain'
   *  Product: '<S38>/Product'
   *  Product: '<S38>/Product1'
   *  Sum: '<S34>/SpeedCount'
   *  UnitDelay: '<S38>/Unit Delay'
   */
  LBG_Arctan_FOC_DW.UnitDelay_DSTATE_f = (real32_T)((int32_T)(uint32_T)
    rtb_Saturation - (int32_T)
    LBG_Arctan_FOC_DW.Delay_DSTATE_m[LBG_Arctan_FOC_DW.CircBufIdx]) *
    6.98491931E-5F * 0.142857149F * 0.02F + 0.98F *
    LBG_Arctan_FOC_DW.UnitDelay_DSTATE_f;

  /* Sum: '<S41>/Add1' incorporates:
   *  Constant: '<S41>/Filter_Constant'
   *  Constant: '<S41>/One'
   *  Gain: '<S31>/f_2_we'
   *  Gain: '<S31>/n_2_f'
   *  Product: '<S41>/Product'
   *  Product: '<S41>/Product1'
   *  UnitDelay: '<S38>/Unit Delay'
   *  UnitDelay: '<S41>/Unit Delay'
   */
  OB_LGB_SPD = 0.116666667F * LBG_Arctan_FOC_DW.UnitDelay_DSTATE_f * 6.28318548F
    * 0.02F + 0.98F * OB_LGB_SPD;

  /* Update for DiscreteIntegrator: '<S87>/Integrator' incorporates:
   *  Gain: '<S84>/Integral Gain'
   */
  LBG_Arctan_FOC_DW.Integrator_PREV_U = 970.0F * rtb_Sum_b;

  /* Update for DiscreteIntegrator: '<S135>/Integrator' incorporates:
   *  Gain: '<S132>/Integral Gain'
   */
  LBG_Arctan_FOC_DW.Integrator_PREV_U_b = 970.0F * rtb_Add3_p;

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* Gain: '<S158>/one_by_two' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   */
  rtb_Sum_b = 0.5F * rtb_Switch_idx_0;

  /* Gain: '<S158>/sqrt3_by_two' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   */
  rtb_Add3_p = 0.866025388F * rtb_Sum4;

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */

  /* Sum: '<S158>/add_b' */
  rtb_Add1_a = rtb_Add3_p - rtb_Sum_b;

  /* Sum: '<S158>/add_c' */
  rtb_Sum_b = (0.0F - rtb_Sum_b) - rtb_Add3_p;

  /* Outputs for Atomic SubSystem: '<S7>/Inverse Park Transform' */
  /* Gain: '<S156>/one_by_two' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   *  MinMax: '<S156>/Max'
   *  MinMax: '<S156>/Min'
   *  Sum: '<S156>/Add'
   */
  rtb_Add3_p = (fmaxf(fmaxf(rtb_Switch_idx_0, rtb_Add1_a), rtb_Sum_b) + fminf
                (fminf(rtb_Switch_idx_0, rtb_Add1_a), rtb_Sum_b)) * -0.5F;

  /* Sum: '<S8>/Add' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S51>/a16'
   *  Constant: '<S8>/Constant'
   *  Gain: '<S155>/Gain'
   *  Gain: '<S8>/Gain'
   *  Sum: '<S155>/Add1'
   *  Sum: '<S155>/Add2'
   *  Sum: '<S155>/Add3'
   */
  arg_Vabc_PU[0] = (rtb_Switch_idx_0 + rtb_Add3_p) * 1.15470052F * 0.0721687824F
    + 0.5F;

  /* End of Outputs for SubSystem: '<S7>/Inverse Park Transform' */
  arg_Vabc_PU[1] = (rtb_Add1_a + rtb_Add3_p) * 1.15470052F * 0.0721687824F +
    0.5F;
  arg_Vabc_PU[2] = (rtb_Add3_p + rtb_Sum_b) * 1.15470052F * 0.0721687824F + 0.5F;

  /* Update for SwitchCase: '<S9>/Switch Case' */
  if (LBG_Arctan_FOC_DW.SwitchCase_ActiveSubsystem == 1) {
    /* Update for IfAction SubSystem: '<S9>/Switch Case Action Subsystem1' incorporates:
     *  ActionPort: '<S168>/Action Port'
     */
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_PREV =
      LBG_Arctan_FOC_DW.FOC_PREV_T;
    LBG_Arctan_FOC_DW.SwitchCaseActionSubsystem1_RESE = false;

    /* End of Update for SubSystem: '<S9>/Switch Case Action Subsystem1' */
  }

  /* Update for DiscreteIntegrator: '<S87>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE = 0U;

  /* Update for DiscreteIntegrator: '<S135>/Integrator' */
  LBG_Arctan_FOC_DW.Integrator_SYSTEM_ENABLE_m = 0U;

  /* Update for UnitDelay: '<S23>/Unit Delay' incorporates:
   *  Delay: '<S10>/Delay'
   */
  LBG_Arctan_FOC_DW.UnitDelay_DSTATE = LBG_Arctan_FOC_DW.Delay_DSTATE_n;

  /* Update for Delay: '<S34>/Delay' incorporates:
   *  Gain: '<S34>/PositionToCount'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE_m[LBG_Arctan_FOC_DW.CircBufIdx] = (uint32_T)
    rtb_Saturation;
  if (LBG_Arctan_FOC_DW.CircBufIdx < 3U) {
    LBG_Arctan_FOC_DW.CircBufIdx++;
  } else {
    LBG_Arctan_FOC_DW.CircBufIdx = 0U;
  }

  /* End of Update for Delay: '<S34>/Delay' */
  /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator4' */

  /* Update for Delay: '<S1>/Delay' incorporates:
   *  UnitDelay: '<S38>/Unit Delay'
   */
  LBG_Arctan_FOC_DW.Delay_DSTATE = LBG_Arctan_FOC_DW.UnitDelay_DSTATE_f;
}

/* Model initialize function */
void LBG_Arctan_FOC_initialize(const char_T **rt_errorStatus, const
  rtTimingBridge *timingBridge, int_T mdlref_TID0, int_T mdlref_TID1, int_T
  mdlref_TID2)
{
  RT_MODEL_LBG_Arctan_FOC_T *const LBG_Arctan_FOC_M =
    &(LBG_Arctan_FOC_MdlrefDW.rtm);

  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* setup the global timing engine */
  LBG_Arctan_FOC_GlobalTID[0] = mdlref_TID0;
  LBG_Arctan_FOC_GlobalTID[1] = mdlref_TID1;
  LBG_Arctan_FOC_GlobalTID[2] = mdlref_TID2;
  LBG_Arctan_FOC_TimingBrdg = timingBridge;

  /* initialize error status */
  rtmSetErrorStatusPointer(LBG_Arctan_FOC_M, rt_errorStatus);
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
